<?php

namespace app\admin\controller\god;

use app\common\controller\Backend;
use think\Db;

// 招生类型  <=> 报读类型
class Enrollment extends Backend
{
    protected $model = null;
    protected $collegeModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model("Enrollment");
        $this->collegeModel = model('College');

//        样式
        $this->assign("style_names", [
            'item1' => '样式1', 'item2' => '样式2', 'item3' => '样式3'
        ]);

//        默认选择样式1
        $this->assign('styleItem', 'item1');

//      推荐信息
        $this->assign('recColleges', $this->collegeModel->column('co_name', 'id'));
        $this->assign('recProfession', model('Major')->column('name', 'id'));
        $this->assign('recCity', model('College')->column('addr_area', 'id'));
// 样式2 推荐的院校
        $this->assign('topColleges', $this->collegeModel->column('co_name', 'id'));
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $result = ['total' => 0, 'rows' => []];
            $list = $this->model->select();
            $result['rows'] = $list;

            return json($result);
        }

        return $this->view->fetch();
    }
    private function getSaveData($args): array
    {
        list($rec_colleges, $rec_profession, $rec_city, $top_colleges, $item3MajorRec, $item3ColRec) = $this->getRecArgs($args);

        return [
            "name" => $args['name'],
            "swiper_pics" => $args['swiper_pics'],
            "rec_colleges" => $rec_colleges,
            "rec_profession" => $rec_profession,
            "rec_city" => $rec_city,
            "introduction" => $args['introduction'],
            "introduction_pics" => $args['introduction_pics'],
            "top_colleges" => $top_colleges,

            'itme3_main_pic' => $args['itme3_main_pic'],
            'itme3_major_pic' => $args['itme3_major_pic'],
            'item3_major_rec' => $item3MajorRec,
            'itme3_col_pic' => $args['itme3_col_pic'],
            'item3_col_rec' => $item3ColRec,
            'itme3_teacher_pic' => $args['itme3_teacher_pic'],
            'item3_detail' => $args['item3_detail'],

            "status" => $args['status']
        ];
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args)) {
                $this->error('参数为空');
            }
            if (empty($args['name'])) {
                $this->error('类型名称不能为空');
            }
            Db::startTrans();
            try {
                $data = $this->getSaveData($args);
                $add = $this->model->data($data)->save();
                if (empty($add)) {
                    throw new \Exception('类型添加失败', -1);
                }

                Db::commit();
            } catch (\Exception $e) {
                if ($e->getCode() === -1) {
                    $this->error($e->getMessage());
                }
                $this->error('服务异常，请稍后重试');
            }
            $this->success('添加成功', '', $add);
        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {

        $row = $this->model->get(['id' => $ids]);
        if (empty($row)) {
            $this->error('参数异常');
        }
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args)) {
                $this->error('参数错误');
            }
            $data = $this->getSaveData($args);
            $save = $this->model->save($data, ['id' => intval($ids)]);
            if (empty($save)) {
                $this->error('更新失败');
            }
            $this->success('更新成功');
        }

        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

    /**
     * @param $args
     * @return array
     */
    public function getRecArgs($args): array
    {
        $rec_colleges = '';
        if (!empty($args['rec_colleges'])) {
            $rec_colleges = join(',', $args['rec_colleges']);
        }
        $rec_profession = '';
        if (!empty($args['rec_profession'])) {
            $rec_profession = join(',', $args['rec_profession']);
        }
        $rec_city = '';
        if (!empty($args['rec_city'])) {
            $rec_city = join(',', $args['rec_city']);
        }
        $top_colleges = '';
        if (!empty($args['top_colleges'])) {
            $top_colleges = join(',', $args['top_colleges']);
        }
        $item3MajorRec = '';
        if (!empty($args['item3_major_rec'])) {
            $item3MajorRec = join(',', $args['item3_major_rec']);
        }
        $item3ColRec = '';
        if (!empty($args['item3_col_rec'])) {
            $item3ColRec = join(',', $args['item3_col_rec']);
        }

        return array($rec_colleges, $rec_profession, $rec_city, $top_colleges, $item3MajorRec, $item3ColRec);
    }

}